package com.jlnk.gsondemo.views;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jlnk.gsondemo.R;
import com.jlnk.gsondemo.adapter.MedalAdapter;
import com.jlnk.gsondemo.entity.OlympicMedal;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class OlympicMedalActivity extends AppCompatActivity {
    private RecyclerView medalRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_olympic_medal);

        medalRecyclerView = findViewById(R.id.medal_recycler_view);

        String jsonData = getJson("olympic.json");
        displayMedalList(jsonData);
    }

    private String getJson(String fileName) {
        StringBuilder json = new StringBuilder();
        try {
            InputStream is = getAssets().open(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = reader.readLine()) != null) {
                json.append(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    private void displayMedalList(String jsonData) {
        if (jsonData == null || jsonData.isEmpty()) {
            medalRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            MedalAdapter adapter = new MedalAdapter(null);
            medalRecyclerView.setAdapter(adapter);
            return;
        }
        Gson gson = new Gson();
        List<OlympicMedal> medalsList;
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            if (!jsonObject.has("result")) {
                medalRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                MedalAdapter adapter = new MedalAdapter(null);
                medalRecyclerView.setAdapter(adapter);
                return;
            }
            JSONArray medalsJsonArray = jsonObject.getJSONObject("result").getJSONArray("medalsList");
            medalsList = gson.fromJson(medalsJsonArray.toString(), new TypeToken<List<OlympicMedal>>() {}.getType());
            if (medalsList == null || medalsList.isEmpty()) {
                medalRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                MedalAdapter adapter = new MedalAdapter(null);
                medalRecyclerView.setAdapter(adapter);
                return;
            }
            medalRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            MedalAdapter adapter = new MedalAdapter(medalsList);
            medalRecyclerView.setAdapter(adapter);
        } catch (Exception e) {
            e.printStackTrace();
            medalRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            MedalAdapter adapter = new MedalAdapter(null);
            medalRecyclerView.setAdapter(adapter);
        }
    }
}